<?php

namespace App\Admin\Controllers;

use App\Models\BlindBox;
use App\Models\Product;
use App\Models\BlindBoxCategory;
use App\Models\BlindBoxLevel;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Str;
use App\Admin\Actions\BlindBoxAction\RefreshBlindBoxRate;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Facades\Storage;
use Encore\Admin\Controllers\AdminController;

class BlindBoxesController extends AdminController
{
    protected $title = 'hotclub盲盒';
    

     protected function blindBox($id = null)
	{
		if ($id and ($blindBox = BlindBox::find($id))) {
			return $blindBox;
		}
		return null;
	}

    protected function grid()
    {
        $grid = new Grid(new BlindBox());
	
	    $grid->disableExport();
	    
	    $grid->column('name', '盲盒名称');
	    $grid->column('image', '封面')->display(function ($value) {
	        if (! Storage::exists($value)) {
		       // $this->update(['cover' => null]);
		        return '';
	        } else {
		        return _grid_image($value);
	        }
        });
        $grid->column('price', '盲盒价格');
	    $grid->column('created_at', '创建时间')->display(function () {
	    	return $this->created_at->toDatetimeString();
	    });
	   	$this->gridActions($grid);
        return $grid;
    }
    

   	protected function gridActions(Grid $grid)
	{
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			$actions->disableDelete();
			$actions->add(new RefreshBlindBoxRate());
		});
	}

    protected function detail($id)
    {
        $show = new Show(BlindBox::findOrFail($id));
        return $show;
    }
    
    protected function form()
    {
        $form = new Form(new BlindBox());

	   	$form->select('blind_box_category_id','盲盒分类')->options(BlindBoxCategory::all()->pluck('name', 'id'));

	    $form->text('name', '盲盒名称');
	 	$form->image('image', '封面图片')
		    ->uniqueName()
		    ->creationRules('required');
		$form->currency('price', '盲盒价格')->symbol('￥')->default(0);

        return $form;
    }

   /* public function batchBlindBoxProductsFrom(Content $content)
	{
		$sid = request('sid');

		if (!$box = BlindBox::find($sid)) {
			admin_toastr('盲盒不存在', 'error');
			return redirect('/admin/blind-boxes');
		}

		$form = new \Encore\Admin\Widgets\Form();

		$form->action(route('admin.blind.boxes.batch.products', ['blind_box_id' => $sid]));

		$form->html("<b>盲盒：</b>{$box->name}");

		$form->select('product_id', '商品')
				->options(Product::all()->pluck('title', 'id'))
				->rules('required');

			$form->select('blind_box_level_id', '等级')
				->options(BlindBoxLevel::all()->pluck('name', 'id'))
				->rules('required');

        $blindBox = $this->blindBox($sid);

		$form->hasMany('blindBox', "商品", function ($form)  {

			 $form->text('grade', '客源分类');
		});

		
		$box= (new Box('批量生成盲盒商品', $form))->style('info');
		

		return $content->header('批量生成盲盒商品')->body($box);
	}

	public function storeBatchBlindBoxProducts()
	{
		if (! BlindBox::find(request('blind_box_id'))) {
			admin_toastr('盲盒不存在', 'error');
			return redirect('/admin/blind-boxes');
		}

		$data = $this->validate(request(), [
			'blind_box_id' => 'required|integer:min:1',
			'product_id' => 'required|integer:min:1',
			'blind_box_level_id' => 'required|integer:min:1',
		], [], [
			'blind_box_id' => '盲盒',
			'product_id' => '商品',
			'blind_box_level_id' => '等级',
		]);

		for ($i = 0; $i < $data['amount']; $i++) {
			GoldProduct::create([
				'gold_seed_id' => $data['gold_seed_id'],
				'gold_round_id' => $data['gold_round_id'],
                'started_at' => Carbon::now()
			]);
		}

		admin_toastr('批量创建成功', 'success');

		return redirect('/admin/gold-seeds');
	}*/

   
}
